package com.zdxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zdxf.compoent.WebSocketServer;
import com.zdxf.conf.Constant;
import com.zdxf.domain.SiteXmlDO;
import com.zdxf.domain.UserDO;
import com.zdxf.param.LogParam;
import com.zdxf.service.impl.ServiceManageServiceImpl;
import com.zdxf.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;

@Api(tags = "组件服务")
@RestController
@RequestMapping("/serviceManage")
public class ServiceManageController {
    @Autowired
    ServiceManageServiceImpl serviceManageService;

    HashMap<String, Object> params = new HashMap<>(Constant.HASH_MAP_SIZE);


    @ApiOperation(value = "获取服务列表", notes = "获取服务列表")
    @ApiImplicitParams({})
    @GetMapping(value = "/listNoStatus")
    public Object getServiceListNoStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new Result(true, 20000, "获取服务列表成功", serviceManageService.getServiceListNoStatus());
    }

    @ApiOperation(value = "获取服务列表（带是否启动状态）", notes = "获取服务列表")
    @ApiImplicitParams({})
    @GetMapping(value = "/list")
    public Object getServiceList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new Result(true, 20000, "获取服务列表成功", serviceManageService.getServiceList());
    }

    @ApiOperation(value = "启动服务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceId", value = "", required = true, paramType = "query"),
    })
    @GetMapping(value = "/start")
    public Object startServiceById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("serviceId"));
        return new Result(true, 20000, "启动服务成功", serviceManageService.startServiceById(id));
    }

    @ApiOperation(value = "停止服务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceId", value = "", required = true, paramType = "query"),
    })
    @GetMapping(value = "/stop")
    public Object stopServiceById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("serviceId"));
        return new Result(true, 20000, "停止服务成功", serviceManageService.stopServiceById(id));
    }

    @ApiOperation(value = "重启服务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceId", value = "", required = true, paramType = "query"),
    })
    @GetMapping(value = "/restart")
    public Object restart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("serviceId"));
        serviceManageService.stopServiceById(id);
        return new Result(true, 20000, "重启服务成功", serviceManageService.startServiceById(id));
    }

    @ApiOperation(value = "获取xml配置文件", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", required = true, paramType = "query"),
    })
    @GetMapping(value = "/getSiteList")
    public Object getSiteList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        return new Result(true, 20000, "获取xml配置文件成功", serviceManageService.getSiteList(id));
    }

    @ApiOperation(value = "修改xml配置文件", notes = "")
    @ResponseBody()
    @PostMapping(value = "/updateSite")
    public Object updateSite(@RequestBody SiteXmlDO body, HttpServletResponse response) throws Exception {
        params.put("id", body.getId());
        params.put("value", body.getValue());
        return new Result(true, 20000, "修改xml配置文件成功", serviceManageService.updateSite(params));
    }

    @ApiOperation(value = "读取日志文件", notes = "")
    @ResponseBody()
    @PostMapping(value = "/log")
    public Object getLog(@RequestBody LogParam logParam) throws Exception {
        if (logParam.getNumLastLineToRead() > 0) {
            return serviceManageService.getLogLine(logParam);
        } else {
            return serviceManageService.getFile(logParam);
        }
    }

    @ApiOperation(value = "读取日志文件列表", notes = "")
    @ResponseBody()
    @GetMapping(value = "/logList")
    public Object logList(@RequestParam int id) throws Exception {
        return serviceManageService.getLogList(id);
    }
}

